{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:38.959003Z",
     "start_time": "2021-08-09T04:02:38.951775Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3.6.9'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from platform import python_version\n",
    "\n",
    "python_version()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:39.119682Z",
     "start_time": "2021-08-09T04:02:39.113968Z"
    }
   },
   "outputs": [],
   "source": [
    "import subprocess"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## call example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:39.589493Z",
     "start_time": "2021-08-09T04:02:39.579745Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# returns return code only\n",
    "subprocess.call([\"ls\", \"-lha\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "note that no exception is raised if the underlying command errors:\n",
    "\n",
    "`bash-script-with-bad-syntax` is a shell script with bad syntax."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:39.934591Z",
     "start_time": "2021-08-09T04:02:39.915493Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "127"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "subprocess.call([\"./bash-script-with-bad-syntax\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## call() example using shell=True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:40.513494Z",
     "start_time": "2021-08-09T04:02:40.248650Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "subprocess.call(\"ls -lha\", shell=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## call example capture standard output and error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:40.754326Z",
     "start_time": "2021-08-09T04:02:40.732686Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "127"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "subprocess.call([\"./bash-script-with-bad-syntax\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## call example, force exception if called process causes error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:41.081291Z",
     "start_time": "2021-08-09T04:02:41.066321Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# if there's no error in the underlying process,\n",
    "# this is just the same as subprocess.call\n",
    "subprocess.check_call([\"ls\",\"-lha\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:41.268539Z",
     "start_time": "2021-08-09T04:02:41.247856Z"
    }
   },
   "outputs": [
    {
     "ename": "CalledProcessError",
     "evalue": "Command '['./bash-script-with-bad-syntax']' returned non-zero exit status 127.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mCalledProcessError\u001b[0m                        Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-10-e87152a9901c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# but unlike call, this throws a Called\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0msubprocess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcheck_call\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"./bash-script-with-bad-syntax\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/usr/lib/python3.6/subprocess.py\u001b[0m in \u001b[0;36mcheck_call\u001b[0;34m(*popenargs, **kwargs)\u001b[0m\n\u001b[1;32m    309\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mcmd\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    310\u001b[0m             \u001b[0mcmd\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpopenargs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 311\u001b[0;31m         \u001b[0;32mraise\u001b[0m \u001b[0mCalledProcessError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mretcode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcmd\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    312\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    313\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mCalledProcessError\u001b[0m: Command '['./bash-script-with-bad-syntax']' returned non-zero exit status 127."
     ]
    }
   ],
   "source": [
    "# but unlike call, this throws a Called\n",
    "subprocess.check_call([\"./bash-script-with-bad-syntax\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## call example, capture stfout and stderr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:41.939712Z",
     "start_time": "2021-08-09T04:02:41.926888Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "output:\n",
      "total 44K\n",
      "drwxr-xr-x  3 felipe felipe 4,0K ago  9 01:02 .\n",
      "drwxr-xr-x 73 felipe felipe 4,0K ago  8 20:00 ..\n",
      "-rwxr-xr-x  1 felipe felipe   41 jun 20  2020 bash-script-with-bad-syntax\n",
      "-rw-r--r--  1 felipe felipe  337 jun 20  2020 cmd.py\n",
      "-rw-r--r--  1 felipe felipe    0 ago  9 01:02 err.txt\n",
      "drwxr-xr-x  2 felipe felipe 4,0K jun 20  2020 .ipynb_checkpoints\n",
      "-rw-r--r--  1 felipe felipe  22K ago  9 01:02 main.ipynb\n",
      "-rw-r--r--  1 felipe felipe    0 ago  9 01:02 out.txt\n",
      "\n",
      "error:\n",
      "./bash-script-with-bad-syntax: line 4: foo: command not found\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import subprocess\n",
    "import sys\n",
    "\n",
    "# create two files to hold the output and errors, respectively\n",
    "with open('out.txt','w+') as fout:\n",
    "    with open('err.txt','w+') as ferr:\n",
    "        out=subprocess.call([\"./bash-script-with-bad-syntax\"],stdout=fout,stderr=ferr)\n",
    "        # reset file to read from it\n",
    "        fout.seek(0)\n",
    "            \n",
    "        print('output:')\n",
    "        print(fout.read())\n",
    "        \n",
    "        # reset file to read from it\n",
    "        ferr.seek(0) \n",
    "        print('error:')\n",
    "        print(ferr.read())                              "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## store output in variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:47.328484Z",
     "start_time": "2021-08-09T04:02:47.320184Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total 52K\n",
      "drwxr-xr-x  3 felipe felipe 4,0K ago  9 01:02 .\n",
      "drwxr-xr-x 73 felipe felipe 4,0K ago  8 20:00 ..\n",
      "-rwxr-xr-x  1 felipe felipe   41 jun 20  2020 bash-script-with-bad-syntax\n",
      "-rw-r--r--  1 felipe felipe  337 jun 20  2020 cmd.py\n",
      "-rw-r--r--  1 felipe felipe   62 ago  9 01:02 err.txt\n",
      "drwxr-xr-x  2 felipe felipe 4,0K jun 20  2020 .ipynb_checkpoints\n",
      "-rw-r--r--  1 felipe felipe  22K ago  9 01:02 main.ipynb\n",
      "-rw-r--r--  1 felipe felipe  464 ago  9 01:02 out.txt\n",
      "\n"
     ]
    }
   ],
   "source": [
    "output = subprocess.check_output([\"ls\",\"-lha\"],universal_newlines=True)\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## run() examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:48.630672Z",
     "start_time": "2021-08-09T04:02:48.617192Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CompletedProcess(args=['ls', '-lha'], returncode=0)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cp = subprocess.run([\"ls\",\"-lha\"])\n",
    "cp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:49.113404Z",
     "start_time": "2021-08-09T04:02:49.102572Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CompletedProcess(args=['ls -lha'], returncode=0)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cp = subprocess.run([\"ls -lha\"],shell=True)\n",
    "cp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:49.717359Z",
     "start_time": "2021-08-09T04:02:49.674083Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CompletedProcess(args=['ls', '-lha'], returncode=0, stdout='total 52K\\ndrwxr-xr-x  3 felipe felipe 4,0K ago  9 01:02 .\\ndrwxr-xr-x 73 felipe felipe 4,0K ago  8 20:00 ..\\n-rwxr-xr-x  1 felipe felipe   41 jun 20  2020 bash-script-with-bad-syntax\\n-rw-r--r--  1 felipe felipe  337 jun 20  2020 cmd.py\\n-rw-r--r--  1 felipe felipe   62 ago  9 01:02 err.txt\\ndrwxr-xr-x  2 felipe felipe 4,0K jun 20  2020 .ipynb_checkpoints\\n-rw-r--r--  1 felipe felipe  22K ago  9 01:02 main.ipynb\\n-rw-r--r--  1 felipe felipe  464 ago  9 01:02 out.txt\\n', stderr='')\n"
     ]
    }
   ],
   "source": [
    "cp = subprocess.run([\"ls\",\"-lha\"], universal_newlines=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n",
    "print(cp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:51.587874Z",
     "start_time": "2021-08-09T04:02:51.578240Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stdout:  \n",
      "stderr:  ls: cannot access 'foo bar': No such file or directory\n",
      "\n"
     ]
    }
   ],
   "source": [
    "cp = subprocess.run([\"ls\",\"foo bar\"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)\n",
    "print(\"stdout: \", cp.stdout)\n",
    "print(\"stderr: \", cp.stderr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:52.573015Z",
     "start_time": "2021-08-09T04:02:52.557645Z"
    }
   },
   "outputs": [
    {
     "ename": "CalledProcessError",
     "evalue": "Command '['ls', 'foo bar']' returned non-zero exit status 2.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mCalledProcessError\u001b[0m                        Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-17-f2920e44b5b2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0msubprocess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"ls\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"foo bar\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcheck\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/usr/lib/python3.6/subprocess.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(input, timeout, check, *popenargs, **kwargs)\u001b[0m\n\u001b[1;32m    436\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mcheck\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mretcode\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    437\u001b[0m             raise CalledProcessError(retcode, process.args,\n\u001b[0;32m--> 438\u001b[0;31m                                      output=stdout, stderr=stderr)\n\u001b[0m\u001b[1;32m    439\u001b[0m     \u001b[0;32mreturn\u001b[0m \u001b[0mCompletedProcess\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprocess\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mretcode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstdout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstderr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    440\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mCalledProcessError\u001b[0m: Command '['ls', 'foo bar']' returned non-zero exit status 2."
     ]
    }
   ],
   "source": [
    "subprocess.run([\"ls\",\"foo bar\"], check=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:02:53.992641Z",
     "start_time": "2021-08-09T04:02:53.986050Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Errno 2] No such file or directory: 'xxxx': 'xxxx'\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    cp = subprocess.run([\"xxxx\",\"foo bar\"], universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n",
    "except FileNotFoundError as e:\n",
    "    print(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## popen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:03:10.765247Z",
     "start_time": "2021-08-09T04:03:10.756308Z"
    }
   },
   "outputs": [],
   "source": [
    "from subprocess import Popen"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:03:11.194881Z",
     "start_time": "2021-08-09T04:03:11.183790Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = Popen([\"ls\",\"-lha\"])\n",
    "p.wait()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:03:11.681372Z",
     "start_time": "2021-08-09T04:03:11.671094Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total 52K\n",
      "drwxr-xr-x  3 felipe felipe 4,0K ago  9 01:02 .\n",
      "drwxr-xr-x 73 felipe felipe 4,0K ago  8 20:00 ..\n",
      "-rwxr-xr-x  1 felipe felipe   41 jun 20  2020 bash-script-with-bad-syntax\n",
      "-rw-r--r--  1 felipe felipe  337 jun 20  2020 cmd.py\n",
      "-rw-r--r--  1 felipe felipe   62 ago  9 01:02 err.txt\n",
      "drwxr-xr-x  2 felipe felipe 4,0K jun 20  2020 .ipynb_checkpoints\n",
      "-rw-r--r--  1 felipe felipe  22K ago  9 01:02 main.ipynb\n",
      "-rw-r--r--  1 felipe felipe  464 ago  9 01:02 out.txt\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "p = Popen([\"ls\",\"-lha\"], stdout=subprocess.PIPE, stderr= subprocess.PIPE, universal_newlines=True)\n",
    "\n",
    "output, errors = p.communicate()\n",
    "\n",
    "print(output)\n",
    "print(errors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:03:12.118854Z",
     "start_time": "2021-08-09T04:03:12.096048Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n",
      "\n",
      "total 52K\n",
      "drwxr-xr-x  3 felipe felipe 4,0K ago  9 01:02 .\n",
      "drwxr-xr-x 73 felipe felipe 4,0K ago  8 20:00 ..\n",
      "-rwxr-xr-x  1 felipe felipe   41 jun 20  2020 bash-script-with-bad-syntax\n",
      "-rw-r--r--  1 felipe felipe  337 jun 20  2020 cmd.py\n",
      "-rw-r--r--  1 felipe felipe   62 ago  9 01:02 err.txt\n",
      "drwxr-xr-x  2 felipe felipe 4,0K jun 20  2020 .ipynb_checkpoints\n",
      "-rw-r--r--  1 felipe felipe  22K ago  9 01:02 main.ipynb\n",
      "-rw-r--r--  1 felipe felipe  464 ago  9 01:02 out.txt\n",
      "\n"
     ]
    }
   ],
   "source": [
    "path_to_output_file = '/tmp/myoutput.txt'\n",
    "\n",
    "myoutput = open(path_to_output_file,'w+')\n",
    "\n",
    "p = Popen([\"ls\",\"-lha\"], stdout=myoutput, stderr= subprocess.PIPE, universal_newlines=True)\n",
    "\n",
    "output, errors = p.communicate()\n",
    "\n",
    "print(output)\n",
    "print(errors)\n",
    "\n",
    "with open(path_to_output_file,\"r\") as f:\n",
    "    print(f.read())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:03:51.454412Z",
     "start_time": "2021-08-09T04:03:51.427234Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "output:  None\n",
      "errors:  None\n",
      "output_file:  ls: cannot access 'foo bar': No such file or directory\n",
      "\n"
     ]
    }
   ],
   "source": [
    "path_to_output_file = '/tmp/myoutput.txt'\n",
    "\n",
    "myoutput = open(path_to_output_file,'w+')\n",
    "\n",
    "p = Popen([\"ls\",\"foo bar\"], stdout=myoutput, stderr= myoutput, universal_newlines=True)\n",
    "\n",
    "output, errors = p.communicate()\n",
    "\n",
    "print(\"output: \", output)\n",
    "print(\"errors: \", errors)\n",
    "\n",
    "with open(path_to_output_file,\"r\") as f:\n",
    "    print(\"output_file: \", f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## pipe commands together"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:03:52.396505Z",
     "start_time": "2021-08-09T04:03:52.367540Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "drwxr-xr-x  2 felipe felipe 4,0K jun 20  2020 .ipynb_checkpoints\n",
      "-rw-r--r--  1 felipe felipe  22K ago  9 01:02 main.ipynb\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from subprocess import Popen,PIPE\n",
    "\n",
    "# this is equivalent to ls -lha | grep \"ipynb\"\n",
    "p1 = Popen([\"ls\",\"-lha\"], stdout=PIPE)\n",
    "p2 = Popen([\"grep\", \"ipynb\"], stdin=p1.stdout, stdout=PIPE, universal_newlines=True)\n",
    "\n",
    "p1.stdout.close()\n",
    "\n",
    "output = p2.communicate()[0]\n",
    "\n",
    "print(output)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## async"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-08-09T04:04:21.284611Z",
     "start_time": "2021-08-09T04:04:21.270764Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[return code: 0]\n",
      "\n",
      "[stdout]\n",
      "total 52K\n",
      "drwxr-xr-x  3 felipe felipe 4,0K ago  9 01:04 .\n",
      "drwxr-xr-x 73 felipe felipe 4,0K ago  8 20:00 ..\n",
      "-rwxr-xr-x  1 felipe felipe   41 jun 20  2020 bash-script-with-bad-syntax\n",
      "-rw-r--r--  1 felipe felipe  337 jun 20  2020 cmd.py\n",
      "-rw-r--r--  1 felipe felipe   62 ago  9 01:02 err.txt\n",
      "drwxr-xr-x  2 felipe felipe 4,0K jun 20  2020 .ipynb_checkpoints\n",
      "-rw-r--r--  1 felipe felipe  23K ago  9 01:04 main.ipynb\n",
      "-rw-r--r--  1 felipe felipe  464 ago  9 01:02 out.txt\n",
      "\n",
      "stderr is empty\n"
     ]
    }
   ],
   "source": [
    "import asyncio\n",
    "\n",
    "proc = await asyncio.create_subprocess_exec(\n",
    "    'ls','-lha',\n",
    "    stdout=asyncio.subprocess.PIPE,\n",
    "    stderr=asyncio.subprocess.PIPE)\n",
    "\n",
    "\n",
    "# if proc takes very long to complete,\n",
    "# the CPUs are free to use cycles for \n",
    "# other processes\n",
    "stdout, stderr = await proc.communicate()\n",
    "\n",
    "print('[return code: '+ str(proc.returncode) +']')\n",
    "if stdout:\n",
    "    print('\\n[stdout]\\n'+str(stdout.decode()))\n",
    "else:\n",
    "    print('stdout is empty')\n",
    "       \n",
    "if stderr:\n",
    "    print(f'\\n[stderr]:\\n'+str(stderr.decode()))\n",
    "else:\n",
    "    print('stderr is empty')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
